//
//  SNHomeTabBar.m
//  SinaVboc
//
//  Created by V on 29/11/2016.
//  Copyright © 2016 仁忄. All rights reserved.
//

#import "SNHomeTabBar.h"


@interface SNHomeTabBar ()

@property (nonatomic, strong) UIButton *centerButton;

@end

@implementation SNHomeTabBar



- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI {
    
    [self addSubview:self.centerButton];
    
    [_centerButton addTarget:self action:@selector(centerButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}



- (void)centerButtonDidClicked:(UIButton *)sender {
    
    NSLog(@"中间按钮被点击");
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    //拿到宽高
    CGFloat width = kScreenWidth / 5;
    CGFloat height = self.bounds.size.height;
    
    NSInteger index = 0;
    
    //遍历tabbar中的按钮,设置它们的frame,留出中间的位置
    for (UIView *subview in self.subviews) {
        
        if ([subview isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            subview.frame = CGRectMake(index * width, 0, width, height);
            
            index += (index == 1 ? 2 : 1);
        }
    }
    
    //设置中间按钮的位置
    _centerButton.frame = CGRectMake(width * 2, 0, width, height);
}


- (UIButton *)centerButton {
    
    if (!_centerButton) {
        
        _centerButton = [[UIButton alloc] init];
        
        [_centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateSelected];
        
        [_centerButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_centerButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateSelected];
    }
    
    return _centerButton;
}

@end



















































